package sg.edu.nus.iss.team2s.jass.gui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Panel;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.awt.Choice;
import java.awt.Button;


import sg.edu.nus.iss.team2s.jass.control.JobAdvertController;
import sg.edu.nus.iss.team2s.jass.control.LoginController;
import sg.edu.nus.iss.team2s.jass.control.MainController;
import sg.edu.nus.iss.team2s.jass.dto.User;
import sg.edu.nus.iss.team2s.jass.util.*;
/**
 *
 * @author Aye Thazin Kyaw
 * @version 1.1
 *
 */


public class JobAdvertMainScreen extends Frame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private Panel pal_search = null;
	private Panel pal_result = null;
	private Label lbl_keyword = null;
	private Label lbl_salaryRange = null;
	private TextField txt_keyword = null;
	private Choice cho_salary = null;
	private Label lbl_postDate = null;
	private Choice cho_postDate = null;
	private Button cmd_search = null;
	private Button cmd_newEntry = null;
	private Button cmd_login = null;
	private JobAdvertDetailScreen detailScreen = null;
	
	
	private ScrollPane slp_results = null;
	private Panel results = null;

	public String getKeyword()
	{
		return getTxt_keyword().getText();
	}
	public String getSalaryRange()
	{
		return getCho_salary().getSelectedItem();
	}
	public String getPostDate()
	{
		return getCho_postDate().getSelectedItem();
	}
	
	/**
	 * This is the default constructor
	 */
	public JobAdvertMainScreen() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1013, 727);
		this.setTitle("JASS Main Screen");

		this.add(getPal_search(), BorderLayout.NORTH);
		this.add(getPal_result(), BorderLayout.CENTER);
		
		CommonUtility.disposeWhenWindowClosing(this);
		CommonUtility.alignScreenCenter(this);
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * This method initializes pal_search	
	 * 	
	 * @return java.awt.Panel	
	 */
	private Panel getPal_search() {
		if (pal_search == null) {
			lbl_postDate = new Label("Post Date:");
			lbl_postDate.setBounds(new Rectangle(370, 104, 71, 30));
			lbl_salaryRange = new Label();
			lbl_salaryRange.setBounds(new Rectangle(27, 104, 81, 30));
			lbl_salaryRange.setText("Salary Range:");
			pal_search = new Panel();
			pal_search.setLayout(null);
			lbl_keyword = new Label("Keyword:");
			lbl_keyword.setBounds(new Rectangle(27, 51, 81, 30));
			pal_search.add(lbl_keyword, null);
			pal_search.setSize(200, 200);
			pal_search.add(lbl_salaryRange, null);
			pal_search.add(getTxt_keyword(), null);
			pal_search.add(getCho_salary(), null);
			pal_search.add(lbl_postDate, null);
			pal_search.add(getCho_postDate(), null);
			pal_search.add(getCmd_search(), null);
			pal_search.add(getCmd_newEntry(), null);
			pal_search.add(getCmd_login(), null);
		}
		return pal_search;
	}
	
	/**
	 * This method initializes pal_search	
	 * 	
	 * @return java.awt.Panel	
	 */
	private Panel getPal_result() {
		if (pal_result == null) {
			pal_result = new Panel();
			pal_result.setLayout(new BorderLayout());
			pal_result.add(getSlp_results(), BorderLayout.CENTER);
		}
		return pal_result;
	}

	/**
	 * This method initializes txt_keyword	
	 * 	
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_keyword() {
		if (txt_keyword == null) {
			txt_keyword = new TextField();
			txt_keyword.setBounds(new Rectangle(133, 51, 660, 30));
		}
		return txt_keyword;
	}

	/**
	 * This method initializes cho_salary	
	 * 	
	 * @return java.awt.Choice	
	 */
	private Choice getCho_salary() {
		if (cho_salary == null) {
			cho_salary = new Choice();
			cho_salary.setBounds(new Rectangle(133, 104, 200, 30));
//			cho_salary.setSize(new Dimension(200,50));
			cho_salary.setFont(new Font(null, Font.BOLD, 15));
			
			cho_salary.addItem("All");
			cho_salary.addItem("<1000"); 
			cho_salary.addItem("1001-2000");
			cho_salary.addItem("2001-3000");
			cho_salary.addItem("3001-4000");
			cho_salary.addItem("4001-5000");
			cho_salary.addItem(">5000");
		}
		return cho_salary;
	}

	/**
	 * This method initializes cho_postDate	
	 * 	
	 * @return java.awt.Choice	
	 */
	private Choice getCho_postDate() {
		if (cho_postDate == null) {
			cho_postDate = new Choice();
			cho_postDate.setBounds(new Rectangle(460, 104, 209, 35));
			cho_postDate.setFont(new Font(null, Font.BOLD, 15));
			
			
			cho_postDate.addItem("All");
			cho_postDate.addItem("within one day");
			cho_postDate.addItem("within one week");
			cho_postDate.addItem("within one month");
		}
		return cho_postDate;
	}

	/**
	 * This method initializes cmd_search	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getCmd_search() {
		if (cmd_search == null) {
			cmd_search = new Button("Search!");
			cmd_search.setFont(new Font(null, Font.BOLD, 15));
			cmd_search.setBounds(new Rectangle(704, 102, 88, 33));
			cmd_search.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				
					JobAdvertSearchList list = new JobAdvertSearchList(results);
					try {
						list.displayResult(JobAdvertController.searchJobAdvert(getKeyword(), getSalaryRange(),getPostDate()));
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					slp_results.validate();


				}
			});
		
		}
		return cmd_search;
	}

	/**
	 * This method initializes cmd_newEntry	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getCmd_newEntry() {
		if (cmd_newEntry == null) {
			cmd_newEntry = new Button("New Entry");
			cmd_newEntry.setFont(new Font(null, Font.BOLD, 30));
			cmd_newEntry.setForeground(java.awt.Color.RED);
			cmd_newEntry.setBounds(new Rectangle(817, 48, 166, 91));
			cmd_newEntry.setEnabled(false);
			cmd_newEntry.setVisible(false);
			cmd_newEntry.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				
					if(detailScreen == null || detailScreen.isVisible() == false){
						detailScreen = new JobAdvertDetailScreen();
					}
					detailScreen.setVisible(true);


				}
			});
		}
		return cmd_newEntry;
	}

	/**
	 * This method initializes cmd_login	
	 * 	
	 * @return java.awt.Button	
	 */
	private Button getCmd_login() {
		if (cmd_login == null) {
			cmd_login = new Button("Login");
			cmd_login.setFont(new Font(null, Font.BOLD, 20));
//			cmd_login.setForeground(java.awt.Color.RED);
			cmd_login.setBounds(new Rectangle(835, 6, 163, 32));
			
			//cmg_login action listener
			cmd_login.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//hide main screen first					
					LoginScreen loginScreen = new LoginScreen(JobAdvertMainScreen.this);									
					loginScreen.display();
					//loginScreen.setVisible(true);
					//loginScreen.setModal(true);
					//hide the Login button after user login
					if(LoginController.checkLoginStatus() == true)
					{
						
						cmd_login.setVisible(false);
						User user=MainController.getUser();		
						
						
						if (user!=null){
							
							if (user.getUserType().equals(String.valueOf(User.JOBADVERTISER))){
							 
								getCmd_newEntry().setVisible(true);
								getCmd_newEntry().setEnabled(true);
								
							}
						}
					}					
				}
			});
		}
		return cmd_login;
	}
	
	
	private ScrollPane getSlp_results() {
		if (slp_results == null) {
			slp_results = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
			results = new Panel();
			
			slp_results.add(results);
			results.setLayout(new GridBagLayout());
			GridBagConstraints gridBagConstraints00 = new GridBagConstraints();
			gridBagConstraints00.gridx = 0;
			gridBagConstraints00.gridy = 0;
			gridBagConstraints00.anchor = GridBagConstraints.NORTH;
			
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridy = 1;
			
			JobAdvertSearchItemTitle title = new JobAdvertSearchItemTitle();
			results.add(title, gridBagConstraints00);

		}
		return slp_results;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
